package classes.view.component 
{
	import classes.controller.DAOSendFlash;
	import classes.controller.DAOSendJS;
	import classes.controller.EDataObject;
	import classes.controller.ESendJS;
	import classes.model.VOIsLocal;
	import classes.model.VOUserInfo;
	
	import com.d4p.custom.defaultButton.DefaultBtn;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Quart;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class PopupGrade extends Sprite
	{
		private var _close:DefaultBtn;
		private var _point1:MovieClip;
		private var _point2:MovieClip;
		
		public function PopupGrade() 
		{
			setLayOut();
			initListener();
			defaultSetting();
			dataListener();
		}
		
		private function setLayOut():void
		{
			this._close		=	this.getChildByName('close_mc')		as DefaultBtn;
			this._point1	=	this.getChildByName('point_mc') 	as MovieClip;
			this._point2	=	this.getChildByName('point2_mc') 	as MovieClip;
		}
		
		private function initListener():void
		{
			_close.addEventListener(MouseEvent.CLICK, closeClick);
		}
		
		private function dataListener():void
		{
			DAOSendJS.getLoadInstance().addEventListener(ESendJS.EVENT_SUBJECTINFO, 	onRoomInfoLoadcomplete);
		}
		
		private function closeClick(e:MouseEvent):void 
		{
			this.dispatchEvent(new EDataObject(["close"], 								EDataObject.EVENT_DATAOBJECT));
		}
		
		private function defaultSetting():void
		{
			this.alpha = 0;			
			_point1.alpha = 0;
			_point2.alpha = 0;
		}
		
		public function data_init():void
		{
			if(VOIsLocal.getLocalInstance().isLocal == "FILE"){
				var loader:URLLoader = new URLLoader();
				loader.addEventListener( Event.COMPLETE,	onDataLoaded);
				try {
					loader.load( new URLRequest('sample/popupGrade.json'));
				}catch ( error:Error ) {
					trace( "Unable to load requested document." );
				}
			}else{
				//real.
				
				data_send();
			}
		}
		
		private function data_send():void
		{
			//popup은 화면에 붙이고나서 이벤트를 발생시킴.  
			var obj:Object = {};
			//DAOSendFlash.getLoadInstance().setData("subjectInfo",VOUserInfo.getDataInstance().data.id);
			DAOSendFlash.getLoadInstance().setData("initSubjectList", obj);
			
		}
		
		
		private function onDataLoaded(e:Event):void
		{
			var jsonParsed:*  = JSON.parse(e.target.data);
			loadSubjectInfo 	= jsonParsed.data[0];
			
			
		}
		
		private function onRoomInfoLoadcomplete(e:ESendJS):void
		{
			loadSubjectInfo = e._obj;
		}
		
		private function set loadSubjectInfo($obj:Object):void 
		{
			var n:String = String($obj.nowRankAll);
			
			for (var j:int = 0; j < n.length; j++) 
			{
				var n_child:POPUPGradeTopNum = new POPUPGradeTopNum();
				n_child.x = j * 19;
				n_child.gotoAndStop(Number(n.slice(j, j + 1)) + 1);
				_point1.addChild(n_child);
			}
			
			_point2.x = _point1.x + _point1.width + 4 - 67;
			_point2.score_txt.text = String($obj.varyRankAll);
			_point2.r_mc.x = _point2.score_txt.x + _point2.score_txt.textWidth + 3;
			
			for (var i:* in $obj.subject) {
				var child_mc:PopupGradeItem = new PopupGradeItem();
				child_mc.x = 235 + i * 74;
				child_mc.y = 260;
				child_mc.data_init($obj.subject[i], i);
				addChild(child_mc);
			}
			
			TweenLite.to(_point1, 0.4, {y:155, alpha:1, ease:Quart.easeOut});
			TweenLite.to(_point2, 0.4, {y:169, alpha:1, ease:Quart.easeOut});
		}
		
	}

}